package com.niiwoo.civet.system.service.dubbo;


import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.system.dto.common.SysConfParamDTO;
import com.niiwoo.civet.system.dto.request.DeleteParamRequestDTO;
import com.niiwoo.civet.system.dto.request.InsertParamRequestDTO;
import com.niiwoo.civet.system.dto.request.QueryParamsRequestDTO;
import com.niiwoo.civet.system.dto.request.UpdateParamRequestDTO;
import com.niiwoo.civet.system.service.SysParamService;
import com.niiwoo.civet.system.service.local.SysParamLocalService;
import com.niiwoo.tripod.consumer.helper.Assert;
import com.niiwoo.tripod.consumer.helper.Exceptions;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * Created by fushengcai on 2017/9/20.
 */
@Service(version = "1.0.0")
public class SysParamServiceImpl implements SysParamService {
    @Autowired
    private SysParamLocalService localParamService;

    @Override
    public int insertParam(InsertParamRequestDTO insertParamRequest) {
        Assert.notNull(insertParamRequest.getParamType(), "ARGS10001", "paramType can't null");
        Assert.notEmpty(insertParamRequest.getParamKey(), "ARGS10001", "paramKey can't null or empty");
        Assert.notEmpty(insertParamRequest.getParamValue(), "ARGS10001", "paramValue can't null or empty");
        Assert.notNull(insertParamRequest.getCreateManager(), "ARGS10001", "createManager can't null");
        if (insertParamRequest.getParamKey().length() > 128) {
            Exceptions.throwBizException("ARGS10001", "paramKey is too long");
        }
        if (insertParamRequest.getParamValue().length() > 1024) {
            Exceptions.throwBizException("ARGS10001", "paramValue is too long");
        }
        if (insertParamRequest.getDescription() != null && insertParamRequest.getDescription().length() > 1024) {
            Exceptions.throwBizException("ARGS10001", "description is too long");
        }
        if (insertParamRequest.getCreateManager() < 1) {
            Exceptions.throwBizException("ARGS10001", "createManager is error");
        }
        return localParamService.insertParam(insertParamRequest.getParamType(), insertParamRequest.getParamKey(), insertParamRequest.getParamValue(), insertParamRequest.getDescription(), insertParamRequest.getCreateManager());
    }

    @Override
    public void updateParam(UpdateParamRequestDTO updateParamRequest) {
        if (updateParamRequest.getParamId() == null || updateParamRequest.getParamId() < 1) {
            Exceptions.throwBizException("ARGS10001", "paramId is error");
        }
        if (updateParamRequest.getUpdateManager() == null || updateParamRequest.getUpdateManager() < 1) {
            Exceptions.throwBizException("ARGS10001", "updateManager is error");
        }
        if (StringUtils.isAllEmpty(updateParamRequest.getParamValue(), updateParamRequest.getDescription())) {
            Exceptions.throwBizException("ARGS10001", "paramValue and description is all empty");
        }
        localParamService.updateParam(updateParamRequest.getParamId(), updateParamRequest.getParamValue(), updateParamRequest.getDescription(), updateParamRequest.getUpdateManager());

    }

    @Override
    public void deleteParam(DeleteParamRequestDTO deleteParamRequest) {
        if (deleteParamRequest.getParamId() == null || deleteParamRequest.getParamId() < 1) {
            Exceptions.throwBizException("ARGS10001", "paramId is error");
        }
        if (deleteParamRequest.getUpdateManager() == null || deleteParamRequest.getUpdateManager() < 1) {
            Exceptions.throwBizException("ARGS10001", "updateManager is error");
        }
        localParamService.deleteParam(deleteParamRequest.getParamId(), deleteParamRequest.getUpdateManager());
    }

    @Override
    public SysConfParamDTO queryParam(Integer paramId) {
        if (paramId == null || paramId < 1) {
            Exceptions.throwBizException("ARGS10001", "paramId is error");
        }
        return localParamService.queryParam(paramId);
    }

    @Override
    public SysConfParamDTO queryParamByKey(String paramKey) {
        if (StringUtils.isEmpty(paramKey)) {
            Exceptions.throwBizException("ARGS10001", "paramKey is null or empty");
        }
        return localParamService.queryParamByKey(paramKey);
    }

    @Override
    public PageResponseDTO<SysConfParamDTO> queryParams(QueryParamsRequestDTO queryParamsRequest) {
        return localParamService.queryParams(queryParamsRequest);

    }


}
